<template>
  <div class="nav-rights">
    <div class="header">
      <span>入金</span>
      <Icon type="ios-arrow-back" size="18" style="position: absolute;left: 10px;" @click="back" />
    </div>
    <div class="nav-right" style="margin-top: 20px;">
      <div class="bill_box">
        <!-- <section class="trade-group merchant-top">
                    <i class="merchant-icon tips"></i>
                    <span class="tips-word">{{$t('uc.finance.recharge.recharge')}}</span>
                </section> -->
        <section>
          <Form :model="form" :rules="rule" ref="form" :label-width="100">
            <FormItem :label="$t('uc.newrecharge.Depositamount')" prop="m">
              <div style="text-align: left;">
                <Input v-model="form.m" type="number" style="width: 150px;"></Input><span style="margin-left: 5px;">MYR</span>
              </div>
            </FormItem>
            <!--            <FormItem :label="$t('uc.newrecharge.Payfiat')" prop="Payfiat">-->
            <!--              <div class="text-left">-->
            <!--              <Input v-model="form.Payfiat" readonly style="width: 250px"></Input>-->
            <!--              <Select style="width: 80px;margin-left: 5px;">-->
            <!--                <Option value="MYR" label="MYR">MYR</Option>-->
            <!--                <Option value="MYR" label="MYR">SGD</Option>-->
            <!--                <Option value="MYR" label="MYR">USD</Option>-->
            <!--              </Select>-->
            <!--                <p class="mt10">{{$t('uc.newrecharge.exchangerate')}} 1MYR = 1.00MYR</p>-->
            <!--              </div>-->
            <!--            </FormItem>-->
            <FormItem :label="$t('uc.newrecharge.Voucher')" prop="filename">
              <div style="text-align: left;">
                <Input v-model="form.filename" readonly style="width: 150px;"></Input>
              </div>
              <div class="text-left" style="width: 350px;">
                <div class="">
                  <div style="text-align: left;position: relative;width: 100%;">
                    <!--                    <Icon type="ios-add-circle-outline" size="24" />-->
                    <Upload
                        ref="upload"
                        :before-upload="beforeUpload"
                        :on-success="assetHandleSuccess"
                        :on-remove="remove"
                        :show-upload-list="true"
                        class="upload"
                        :data="form"
                        action="https://www.navikex.com/api/updata">
                      <Button>{{$t('uc.newrecharge.upTip')}}</Button>
                    </Upload>
                  </div>
                </div>
                <div class="mt10" style="width: 180px;">
                  <p class="mt10">{{$t('uc.newrecharge.tip')}}</p>
                </div>
                <Button class="mt10" type="primary" style="width: 120px;" @click="submit('form')">{{$t('uc.newrecharge.submit')}}</Button>
              </div>
            </FormItem>
          </Form>
        </section>
      </div>
    </div>
  </div>
</template>
<script>
import Vue from "vue";
import VueQriously from "vue-qriously";
Vue.use(VueQriously);

export default {
  components: {
    VueQriously
  },
  inject: ['reload'],
  data() {
    return {
      form: {
        m: null,
        filename: null,
        url: null
      },
      rule: {
        m: [
          {
            required: true,
            message: this.$t("uc.newrecharge.rulem"),
            trigger: "blur"
          }
        ],
        filename: [
          {
            required: true,
            message: this.$t("uc.newrecharge.rulefile"),
            trigger: "blur"
          }
        ]
      },
      accountType: 0,
      memoCode: "",
      minRechargeAmount:"0.001",
      isShowGetAddress: false,
      isShowEwm: false,
      dataCount: 0,
      loading: true,
      qrcode: {
        value: "",
        size: 220,
        coinName: "",
        unit: ""
      },
      coinType: "",
      coinList: [],
      tableRecharge: [],
      allTableRecharge: []
    };
  },
  methods: {
    back() {
      this.$router.back()
    },
    changePage(index) {
      this.getList(index);
    },
    beforeUpload(file) {
      this.form.file = file
      console.log(this.form.file)
      this.form.filename = file.name
      // return false
    },
    assetHandleSuccess(res, file, fileList) {
      console.log(res)
      this.form.url = res.url
    },
    remove(){
      console.log('remove')
      this.form.filename = null
    },
    submit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          const data = {
            token: sessionStorage.getItem('token'),
            m: this.form.m,
            url: this.form.url
          }
          console.log(data)
          this.$http
              .post("/api/income", data)
              .then(response => {
                var resp = response.body;
                console.log(resp)
                if (resp.code === 200) {
                  this.$Message.success('操作成功')
                  this.$refs[name].resetFields();
                } else {
                  this.$Message.error(resp.msg)
                }
              });

        } else {
        }
      });
    },
    getCurrentCoinRecharge() {
      if (this.coinType != "") {
        var temp = [];
        for (var i = 0; i < this.allTableRecharge.length; i++) {
          if (this.allTableRecharge[i].symbol == this.coinType) {
            temp.push(this.allTableRecharge[i]);
          }
        }
        this.tableRecharge = temp;
      } else {
        this.tableRecharge = this.allTableRecharge;
      }
    },
    showEwm() {
      this.isShowEwm = !this.isShowEwm;
    },
    onCopy(e) {
      this.$Message.success(
          this.$t("uc.finance.recharge.copysuccess") + e.text
      );
    },
    onError(e) {
      this.$Message.error(this.$t("uc.finance.recharge.copysuccess"));
    },
    changeCoin(value) {
      for (var i = 0; i < this.coinList.length; i++) {
        if (this.coinList[i].coin.unit == value) {
          this.qrcode.value = this.coinList[i].address;
          this.qrcode.coinName = this.coinList[i].coin.name.toLowerCase();
          this.qrcode.unit = this.coinList[i].coin.unit;
          this.minRechargeAmount = this.coinList[i].coin.minRechargeAmount;
          if(this.coinList[i].coin.accountType == 1) {
            this.qrcode.value = this.coinList[i].coin.depositAddress;
            this.memoCode = this.coinList[i].memo;
            this.accountType = 1;
          }else{
            this.accountType = 0;
          }

          if(this.qrcode.value == "" || this.qrcode.value == null || this.qrcode.value == undefined){
            this.isShowGetAddress = true;
          }else{
            this.isShowGetAddress = false;
          }
        }
      }
      this.getCurrentCoinRecharge();
    },
    resetAddress(){
      var self = this;
      if(this.qrcode.value == "" || this.qrcode.value == null || this.qrcode.value == undefined){
        this.$Spin.show({
          render: (h) => {
            return h('div', [
              h('Icon', {
                'class': 'demo-spin-icon-load',
                props: {
                  type: 'ios-loading',
                  size: 18
                }
              }),
              h('div', {style:{
                  fontSize: "12px",
                  marginTop: "8px"
                }}, this.$t('uc.finance.recharge.gettingaddress'))
            ])
          }
        });
        let params = {};
        params["unit"] = this.qrcode.unit;
        this.$http
            .post(this.host + "/uc/asset/wallet/reset-address", params)
            .then(response => {
              var resp = response.body;
              if (resp.code == 0) {
                setTimeout(function() {
                  self.reload();
                  self.$Spin.hide();
                }, 3000);
              } else {
                this.$Message.error(resp.message);
                self.$Spin.hide();
              }
            });
      }
    },
    getMoney() {
      //获取
      this.$http.post(this.host + this.api.uc.wallet).then(response => {
        var resp = response.body;
        if (resp.code == 0) {
          for (let i = 0; i < resp.data.length; i++) {
            var coin = resp.data[i];
            if (coin.coin.canRecharge == 1) {
              this.coinList.push(coin);
            }
          }
          this.changeCoin(this.coinType);
        } else {
          this.$Message.error(resp.message);
        }
      });
    },
    getList(pageN) {
      //获取tableRecharge
      let memberId = 0;
      !this.$store.getters.isLogin && this.$router.push("/login");
      this.$store.getters.isLogin && (memberId = this.$store.getters.member.id);
      let pageNo = pageN,
          pageSize = 10,
          type = 0,
          params = { memberId, pageNo, pageSize, type };
      this.$http.post(this.host + "/uc/asset/transaction/all", params).then(response => {
        var resp = response.body;
        if (resp.code == 0) {
          if (resp.data) {
            let trueData = resp.data;
            this.allTableRecharge = trueData.content || [];
            this.dataCount = trueData.totalElements || 0;
            this.getCurrentCoinRecharge();
          }
          this.loading = false;
        } else {
          this.$Message.error(resp.message);
        }
      });
    },
    getMember() {
      let self = this;
      this.$http.post(this.host + "/uc/approve/security/setting").then(response => {
        var resp = response.body;
        if (resp.code == 0) {
          if (resp.data.realName == null || resp.data.realName == "") {
            // 判断是否实名认证，未认证跳转到实名认证页面；
            this.$Message.success(this.$t("otc.publishad.submittip1"));
            self.$router.push("/uc/safe");
          } else if (resp.data.phoneVerified == 0) {
            // 判断是否是手机号0，1，未认证跳转到实名认证页面；
            this.$Message.success(this.$t("otc.publishad.submittip2"));
            self.$router.push("/uc/safe");
          } else if (resp.data.fundsVerified == 0) {
            // 判断是否设置交易密码，未认证跳转到实名认证页面；
            this.$Message.success(this.$t("otc.publishad.submittip3"));
            self.$router.push("/uc/safe");
          }
        } else {
          this.$Message.error(resp.message);
        }
      });
    }
  },
  created() {
    this.coinType = this.$route.query.name || "";
  },
  computed: {
    tableColumnsRecharge() {
      let columns = [];
      columns.push({
        title: this.$t("uc.finance.recharge.time"),
        align: "center",
        width: 200,
        render: (h, param) => {
          let str = param.row.createTime;
          return h("div", {}, str);
        }
      });
      columns.push({
        title: this.$t("uc.finance.recharge.symbol"),
        align: "center",
        width: 120,
        render: (h, param) => {
          let str = param.row.symbol;
          return h("div", {}, str);
        }
      });
      columns.push({
        title: this.$t("uc.finance.recharge.address"),
        align: "center",
        render: (h, param) => {
          let str = param.row.address;
          return h("div", {}, str);
        }
      });
      columns.push({
        title: this.$t("uc.finance.recharge.amount"),
        align: "center",
        width: 100,
        render: (h, param) => {
          let str = param.row.amount;
          return h("div", {}, str);
        }
      });
      return columns;
    }
  }
};
</script>
<style scoped>
.text-left{
  text-align: left;
}
.mt10 {
  margin-top: 10px;
}
.upload-div {
  width: 340px;
  height: 200px;
  background-color: #282828;
  color: #757575;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}
.upload {
  /*width: 100%;*/
  /*height: 100%;*/
  /*position: absolute;*/
  /*left: 0;*/
  /*top: 0;*/
  /*z-index: 999;*/
}
.table-inner {
  position: relative;
  text-align: left;
  border-radius: 3px;
}

.acb-p1 {
  font-size: 16px;
  font-weight: 400;
  line-height: 50px;
}

.acb-p2 {
  font-size: 13px;
  line-height: 24px;
  color: #8c979f;
}

.action-inner {
  width: 100%;
  display: table;
}

.action-inner .inner-box {
  display: table-cell;
  width: 100%;
}

.action-box .title .copy {
  user-select: text;
}

.action-box .title a.link-copy {
  font-size: 14px;
  margin-left: 20px;
  color: #f0a70a;
}

.hb-night a {
  text-decoration: none;
  color: #7a98f7;
  transition: all 0.2s ease-in-out;
  cursor: pointer;
}

.action-box .title a.link-qrcode {
  margin-left: 20px;
  font-size: 14px;
  position: relative;
  color: #f0a70a;
}

.hb-night a {
  text-decoration: none;
  color: #7a98f7;
  transition: all 0.2s ease-in-out;
  cursor: pointer;
}

.action-box .subtitle {
  font-size: 12px;
  margin-top: 30px;
}

.action-content {
  width: 100%;
  margin-top: 30px;
  /* overflow: hidden; */
  display: table;
  color: #ccc;
}

.action-box .title {
  margin-top: 20px;
  font-size: 20px;
  user-select: none;
}

.action-box .title .show-qrcode {
  position: absolute;
  top: -100px;
  left: 40px;
  padding: 10px;
  background: #FFF;
}

.action-inner .inner-box.deposit-address {
  width: 80%;
}

p.describe {
  font-size: 16px;
  font-weight: 600;
}
.merchant-icon {
  display: inline-block;
  margin-left: 4px;
  background-size: 100% 100%;
}

.merchant-icon.tips {
  width: 4px;
  height: 22px;
  margin-right: 10px;
  background: #f0a70a;
}

.bill_box {
  width: 100%;
  height: auto;
  /* overflow: hidden; */
}

.nav-right {
  height: auto;
  overflow: hidden;
  padding: 0 15px;
}

.order_box {
  width: 100%;
  background: #fff;
  height: 56px;
  line-height: 56px;
  margin-bottom: 20px;
  border-bottom: 2px solid #ccf2ff;
  position: relative;
  text-align: left;
}

.order_box a {
  color: #8994a3;
  font-size: 16px;
  padding: 0 30px;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  line-height: 54px;
  display: inline-block;
}

.order_box .search {
  position: absolute;
  width: 300px;
  height: 32px;
  top: 12px;
  right: 0;
  display: flex;
  /* border: #c5cdd7 solid 1px; */
}
.header {
  text-align: center;
  position: relative;
  padding: 10px;
}
</style>
